﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;
using DataObject.Database;

public partial class Pages_Reservation_Sticky : System.Web.UI.Page
{
    private DateTime StickyDate { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["StickyDate"] != null)
        {
            StickyDate = DateTime.Parse(Request["StickyDate"]);
        }
        else StickyDate = DateTime.Today;
        txtStickyDate.Text = StickyDate.ToShortDate();

        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        StickyEntity item = StickyDAO.CreateInstant().GetStickyByDate(StickyDate);
        if (item != null)
        {
            hdfStickyID.Value = item.StickyID.ToString();
            txtStickyDate.Text = item.StickyDate.ToShortDate();
            txtDescription.Text = item.Description;
        }
        else
        {

        }
    }

    private StickyEntity GetStickyEntity()
    {
        StickyEntity item = StickyDAO.CreateInstant().GetStickyByDate(StickyDate);
        if (item == null)
        {
            item = new StickyEntity();
            item.StickyID = Guid.NewGuid();
            item.StickyDate = DateTime.Parse(this.txtStickyDate.Text);
        }
        item.Description = txtDescription.Text;
        return item;
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        StickyEntity item = GetStickyEntity();
        if (!hdfStickyID.Value.Trim().Equals(string.Empty))
            StickyDAO.CreateInstant().Update(item);
        else
            StickyDAO.CreateInstant().Add(item);
        Response.Redirect("Sticky.aspx?StickyDate=" + item.StickyDate);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        StickyEntity item = GetStickyEntity();
        if (!hdfStickyID.Value.Trim().Equals(string.Empty))
        {
            try
            {
                StickyDAO.CreateInstant().Delete(s => s.StickyID.Equals(item.StickyID));
                Response.Redirect("Calendar.aspx");
            }
            catch (Exception ex)
            {
                
            }
        }
    }
}
